/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Account;
import client.entity.Recruitment;
import client.entity.SaveRecruitment;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class SaveMyJobBean {

    private String status="fail";
    
    public SaveMyJobBean() {
    }

    public String saveJobAction() {
        String param = getParam("idRecruitment");
        Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
        if (ac == null) {
            return "login.xhtml?faces-redirect=true";
        } else {
            if (param == null) {
                param = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("idRecruitment");
            } else {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("idRecruitment", param);
            }
            if(ac.getRole()==true){
               FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You are not Worker", "Please Try Again");
                  FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }else{
                SessionFactory factory = HibernateUtil.getSessionFactory();
            Session ss = factory.openSession();
            ss.getTransaction().begin();
            SaveRecruitment saveRecrui= new SaveRecruitment();
            Recruitment recruit= (Recruitment) ss.createQuery("from Recruitment rc where rc.id="+Integer.parseInt(param)).uniqueResult();
            saveRecrui.setAccount(ac);
            saveRecrui.setRecruitment(recruit);
            ss.save(saveRecrui);
            ss.getTransaction().commit();
            ss.close();
            status="true";
        //    return "home.xhtml?faces-redirect=true";
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Information", "Save Job Success");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
            }            
        }
    }

    public String getParam(String param) {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }
    public void saveJobListener(){
        if(status.equals("fail")){
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You are not Worker", "Please Try Again");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
}
